在SpringBoot项目的POM文件中添加spring 您所在的位置:网站首页 build processor 在SpringBoot项目的POM文件中添加spring

在SpringBoot项目的POM文件中添加spring

2024-01-28 17:43| 来源: 网络整理| 查看: 265

写在前面

在自定义starter时,一般都会添加spring-boot-configuration-processor依赖,这是为什么呢?它的作用是什么? 答:①. 将自定义的配置类生成配置元数据,所以在引用自定义STARTER的工程的YML文件中,给自定义配置初始化时,会有属性名的提示。②. 确保在使用@ConfigurationProperties注解时,可以优雅的读取配置信息,引入该依赖后,IDEA不会出现“spring boot configuration annotation processor not configured”的错误。 本文只阐述如何将自定义的配置类生成配置元数据。

问题描述

在实际项目中发现“SPRING相关配置”在点击的时候可以跳转到对应的定义类中,而自定义的“MINIO相关配置”却无法点击。如下:spring.datasource.type可以点击,而minio.url无法点击

# SPRING相关配置 spring: # 数据源 datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: org.postgresql.Driver url: jdbc:postgresql://localhost:5432/hello username: root password: 123456 # MINIO相关配置 minio: url: http://localhost:9000 accessKey: root secretKey: 123456 bucketName: hello-world 问题分析

之所以能够点击,是因为在spring-configuration-metadata.json文件中生成了配置的元数据,为什么自定义的未生成呢?是因为自定义的配置所在工程的pom文件中没有添加如下依赖:

org.springframework.boot spring-boot-configuration-processor true

加入该依赖后执行:mvn clean package,可以看到预期结果,生成了spring-configuration-metadata.json,spring-boot-configuration-processor的作用是在工程编译时,根据配置类自动生成配置类的元数据信息,即META-INF目录下的spring-configuration-metadata.json文件,从而可以告诉Spring容器这个JAR包中有哪些自定义的配置,JSON文件如下: 生成的spring-configuration-metadata.json文件

官方连接: spring-boot-configuration-processor官网连接

原文如下: You can easily generate your own configuration metadata file from items annotated with @ConfigurationProperties by using the spring-boot-configuration-processor jar. The jar includes a Java annotation processor which is invoked as your project is compiled.



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有